//
//  RootViewController.m
//  GR8_Iphl
//
//  Created by Tim Linsen on 9/01/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

#import "RootViewController.h"

#import "ManageDatabase.h"
#import "MainMenuViewController.h"

@implementation RootViewController
@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;
@synthesize loginIndicator;
@synthesize lbl;
@synthesize manageDatabase;
@synthesize mainMenuViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    /*[scrollView setScrollEnabled:YES];
    [scrollView setContentSize:CGSizeMake(250,620)];
    [scrollView setPagingEnabled:YES];*/
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    /*self.navigationItem.hidesBackButton = YES;
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
	{
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        usernameField.text = [array objectAtIndex:0];
        
            }
    UIApplication *app = [UIApplication sharedApplication];
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(applicationWillResignActive:)
                                                 name:UIApplicationWillResignActiveNotification 
                                               object:app];
    // Do any additional setup after loading the view from its nib.
     */
}
- (void) keyboardWillShow: (NSNotification*) aNotification;
{	
	
	[UIView beginAnimations:nil context:NULL];
	
    [UIView setAnimationDuration:0.3];
	
    CGRect rect = [[self view] frame];
	
    rect.origin.y -= 200; 
    
    [[self view] setFrame: rect];
    
	[UIView commitAnimations];
	
}

- (void) keyboardWillHide: (NSNotification*) aNotification;
{
	[UIView beginAnimations:nil context:NULL];
	
    [UIView setAnimationDuration:0.3];
	
    CGRect rect = [[self view] frame];
	
    rect.origin.y += 200; 
    
    [[self view] setFrame: rect];
	
	[UIView commitAnimations];
}
-(IBAction) backgroundTap:(id) sender{
    [self.usernameField resignFirstResponder];
    [self.passwordField resignFirstResponder];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	[usernameField resignFirstResponder];
    [passwordField resignFirstResponder];
}

- (IBAction) login: (id) sender
{
    loginIndicator.hidden = FALSE;
	[loginIndicator startAnimating];
	
        self.mainMenuViewController = [[MainMenuViewController alloc] 
                                initWithNibName:@"MainMenuViewController" bundle:nil];
        [self.navigationController pushViewController:self.mainMenuViewController animated:YES];

}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
